package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Cart;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.wx.vo.CartCheckOutBO;
import com.cskaoyan.bean.wx.vo.CartCheckOutVO;
import com.cskaoyan.bean.wx.vo.CartIndexVO;
import com.cskaoyan.service.CartService;
import com.cskaoyan.utils.Constant;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @PROJECT_NAME: market_34th
 * @DESCRIPTION:
 * @Author: qiu
 * @DATE: 2021/10/22 10:32
 */
@RestController
@RequestMapping("/wx/cart")
public class CartController {

    @Autowired
    CartService cartService;

    /**
     * 购物车首页
     * @return
     */
    @RequestMapping("index")
    public BaseRespVo index() {

        CartIndexVO cartIndexVO = cartService.getIndex();

        return BaseRespVo.ok(cartIndexVO);
    }


    /**
     * 修改购物车商品数量
     * @param cart
     * @return
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Cart cart) {

        if (cart != null && cart.getId() >= 0) {
            cartService.update(cart);
        }

        return BaseRespVo.ok();
    }

    /**
     * 选中或取消选中购物车商品
     * @param map
     * @return
     */
    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody Map map) {

        Integer ischecked = (Integer) map.get("isChecked");
        List<Integer> productIds = ((List<Integer>) map.get("productIds"));

        CartIndexVO cartIndexVO = cartService.checked(ischecked, productIds);

        return BaseRespVo.ok(cartIndexVO);
    }

    /**
     * 将某商品+规格加入购物车；已做登录判断
     * @param cart
     * @return
     */
    @PostMapping("add")
    public BaseRespVo add(@RequestBody Cart cart) {

        int total = -1;

        if (cart != null && cart.getGoodsId() > 0) {
            total = cartService.add(cart);  // 当前用户的购物车的该商品的数量
        }

        if(total < 0){
            return BaseRespVo.ok(501, null, "请登录");
        }
        return BaseRespVo.ok(total);    // 返回的是当前用户的购物车的该商品的数量
    }

    /**
     * 加入购物车（通过点击立即购买产生的）；已做登录判断
     */
    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody Cart cart) {

        int cartId = -1;

        // 注：如果立即购买的产品是购物车中已经有的，会将现有的numbers覆盖进去
        if (cart != null && cart.getGoodsId() >= 0) {
            cartId = cartService.fastadd(cart);  // 商品对应的cartId
        }

        if(cartId < 0){
            return BaseRespVo.ok(501, null, "请登录");
        }

        // 返回的是商品对应的cartId
        return BaseRespVo.ok(cartId);
    }


    /**
     * 确认订单页面
     * @return
     */
    @GetMapping("/checkout")
    public BaseRespVo checkout(CartCheckOutBO cartCheckOutBO) {

        CartCheckOutVO vo = cartService.checkout(cartCheckOutBO);

        return BaseRespVo.ok(vo);
    }

    /**
     * 统计购物车中产品总数量（包括未选中的）
     * @return
     */
    @GetMapping("/goodscount")
    public BaseRespVo goodscount() {

        Integer count = cartService.goodscount();

        return BaseRespVo.ok(count);
    }

    /**
     * 删除购物车选中的商品，返回删除后的购物车状态
     * @return
     */
    @PostMapping("/delete")
    public BaseRespVo delete(@RequestBody Map map) {

        List<Integer> productIds = (ArrayList<Integer>) map.get("productIds");

        CartIndexVO cartIndexVO = cartService.delete(productIds);

        return BaseRespVo.ok(cartIndexVO);
    }


}